package com.jianhua.backend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianhua.backend.domain.dto.LoginDto;
import com.jianhua.backend.domain.dto.RegisterDto;
import com.jianhua.backend.domain.dto.ResetPasswordDto;
import com.jianhua.backend.domain.entity.Admin;
import com.jianhua.backend.domain.vo.LoginVo;
import com.jianhua.backend.mapper.AdminMapper;
import com.jianhua.backend.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 存储系统管理员用户的基本信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-02
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public LoginVo login(LoginDto loginDto) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, loginDto.getUsername());
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin == null){
            //todo: 抛出用户名不存在异常
        }
        if(!admin.getPassword().equals(loginDto.getPassword())){
            //todo: 抛出密码不正确异常
        }
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(admin, loginVo);
        return loginVo;
    }

    @Override
    public void register(RegisterDto registerDto) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, registerDto.getUsername());
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin != null){
            //todo: 抛出用户名已存在异常
        }
        if(!registerDto.getPassword().equals(registerDto.getOldPassword())){
            //todo: 抛出密码与确认密码不相同异常
        }
        Admin admin1 = new Admin();
        BeanUtils.copyProperties(registerDto, admin1);
        adminMapper.insert(admin1);
    }

    @Override
    public void resetPassword(ResetPasswordDto resetPasswordDto) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, resetPasswordDto.getUsername());
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin == null){
            //todo: 抛出用户名不存在异常
        }
//        if(!admin.getSecurityQuestion.equals(resetPasswordDto.getSecurityQuestion())){
//            //todo: 抛出安全问题不正确异常
//        }
        if(!resetPasswordDto.getPassword().equals(resetPasswordDto.getOldPassword())){
            //todo: 抛出密码与确认密码不相同异常
        }
        admin.setPassword(resetPasswordDto.getPassword());
        adminMapper.updateById(admin);
    }
}
